﻿using System.Text.Json;

using Microsoft.AspNetCore.Mvc;

using XServer.Managers;
using XServer.Models.DrawCards;

using XServer.Systems;

namespace XServer.Controllers
{
    [ApiController]
    [Route("/gacha")]
    public class DrawCardsController : ControllerBase
    {
        private DrawCardsSystem _drawCardsSystem;
        private UserManager _userManager;

        public DrawCardsController(DrawCardsSystem drawCardsSystem, UserManager userManager)
        {
            _drawCardsSystem = drawCardsSystem;
            _userManager = userManager;
        }

        /// <summary>
        /// 抽卡页面
        /// </summary>
        /// <returns></returns>
        [HttpGet("/gacha")]
        public IActionResult GachaView()
        {
            string filePath = "./web/index.html";
            return Content(System.IO.File.ReadAllText(filePath), "text/html");
        }

        /// <summary>
        /// 抽卡接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("/gacha/drawcard")]
        public IActionResult Post([FromBody] DrawCardsRequest request)
        {
            // 注册用户
            _userManager.RegistUser(request.uid);

            var cards = _drawCardsSystem.ProcessPulls(request.cardPoolType, request.drawCount, out int isup, request.uid);

            DrawCardsResponse response = new DrawCardsResponse();
            response.id = request.uid;
            response.drawCount = request.drawCount;
            response.currency = request.currency;
            response.cardPoolType = request.cardPoolType;

            if (cards == null)
            {
                response.code = 503;
                response.msg = "卡池类型不存在";
            }
            else
            {
                response.cards = cards;
                response.code = 200;
                response.msg = isup > 0 ? "金色传说" : "下次一定";
                response.gachaResult = isup;
            }

            return Content(JsonSerializer.Serialize(response), "application/json");
        }

        [HttpGet("/gacha/user/{uid}")]
        public IActionResult Get(string uid, CardPoolType cardPoolType)
        {
            // 注册用户
            _userManager.RegistUser(uid);

            var cardPool = _drawCardsSystem.GetCardPoolInfos(uid, cardPoolType);

            DrawCardsQueryResponse response = new DrawCardsQueryResponse();
            if (cardPool != null)
            {
                response.totalPulls = cardPool.totalPulls;
                response.hardPityRemaining = cardPool.hardPityRemaining;
                response.isPityActive = cardPool.isPityActive;
                response.crookedPity = cardPool.crookedPity;
                response.fiveStarUpCount = cardPool.fiveStarUpCount;
                response.recidentCount = cardPool.recidentCount;
                response.fiveStarCount = cardPool.fiveStarCount;

                response.code = 200;
                response.msg = "";
            }
            else
            {
                response.code = 503;
                response.msg = "卡池类型不存在";
            }


            return Content(JsonSerializer.Serialize(response), "application/json");
        }
    }
}
